/*
 * @Descripttion: 导出类公共方法
 * @version: 1.0.0
 * @Author: liu.yihu
 * @Date: 2020-08-06 13:38:45
 * @LastEditors: liu.yihu
 * @LastEditTime: 2020-08-06 16:21:11
 */
import axios from 'axios';
import store from '@/store/index';
import {
    getSearchDataForm
} from '@comp/common/common';

const service = axios.create({
    baseURL: store.state.baseUrl,
    // timeout: 30000, // 请求超时时间
    withCredentials: false
});
export const DownLoadMixin = {
    data() {
        return {
            downloadLoading: false,
            checkOutData: []
        };
    },
    created() {
    },
    methods: {
        async checkOut() {
            this.downloadLoading = true;
            let obj = getSearchDataForm(this.formData);
            if (this.otherParams) {
                obj = {
                    ...obj,
                    ...this.getOtherParams()
                };
            }
            await service
                .post(this.url, obj)
                .then(res => {
                    this.downloadLoading = false;
                    this.checkOutData = res.data.data;
                }).catch(() => {
                    this.$message.error('导出失败');
                    this.downloadLoading = false;
                });
            return this.checkOutData;
        },
        //适用于分页查询  analysisResult需要解析的字段
        async checkOut_paging() {
            this.downloadLoading = true;
            let obj = getSearchDataForm(this.formData);
            if (this.otherParams) {
                obj = {
                    ...obj,
                    ...this.getOtherParams()
                };
            }
            await service
                .post(this.url, obj)
                .then(res => {
                    this.downloadLoading = false;
                    this.checkOutData = this.analysisResult(res.data.data.data);
                }).catch(() => {
                    this.$message.error('导出失败');
                    this.downloadLoading = false;
                });
            return this.checkOutData;
        },
        //和上面checkOut_paging方法适用于在一个页面多个导出按钮
        async checkOut_paging1() {
            this.downloadLoading = true;
            let obj = getSearchDataForm(this.formData);
            if (this.otherParams) {
                obj = {
                    ...obj,
                    ...this.getOtherParams()
                };
            }
            await service
                .post(this.url1, obj)
                .then(res => {
                    this.downloadLoading = false;
                    this.checkOutData = res.data.data.data;
                }).catch(() => {
                    this.$message.error('导出失败');
                    this.downloadLoading = false;
                });
            return this.checkOutData;
        },
        async checkOut_paging2() {
            this.downloadLoading = true;
            let obj = getSearchDataForm(this.formData);
            if (this.otherParams) {
                obj = {
                    ...obj,
                    ...this.getOtherParams()
                };
            }
            await service
                .post(this.url2, obj)
                .then(res => {
                    this.downloadLoading = false;
                    this.checkOutData = this.analysisResult2(res.data.data.data);
                }).catch(() => {
                    this.$message.error('导出失败');
                    this.downloadLoading = false;
                });
            return this.checkOutData;
        },
        async checkOut_paging3() {
            this.downloadLoading = true;
            let obj = getSearchDataForm(this.formData);
            if (this.otherParams) {
                obj = {
                    ...obj,
                    ...this.getOtherParams()
                };
            }
            await service
                .post(this.url3, obj)
                .then(res => {
                    this.downloadLoading = false;
                    this.checkOutData =  this.analysisResult3(res.data.data.data);
                }).catch(() => {
                    this.$message.error('导出失败');
                    this.downloadLoading = false;
                });
            return this.checkOutData;
        },
        async checkOut_paging4() {
            this.downloadLoading = true;
            let obj = getSearchDataForm(this.formData);
            if (this.otherParams) {
                obj = {
                    ...obj,
                    ...this.getOtherParams()
                };
            }
            await service
                .post(this.url, obj)
                .then(res => {
                    this.downloadLoading = false;
                    this.checkOutData = this.analysisResult(res.data.data);
                }).catch(() => {
                    this.$message.error('导出失败');
                    this.downloadLoading = false;
                });
            return this.checkOutData;
        },
        async checkOut1() {
            this.downloadLoading = true;
            let obj = getSearchDataForm(this.formData);
            if (this.otherParams) {
                obj = {
                    ...obj,
                    ...this.getOtherParams()
                };
            }
            await service
                .post(this.url1, obj)
                .then(res => {
                    ;
                    this.downloadLoading = false;
                    this.checkOutData = res.data.data;
                }).catch(() => {
                    this.$message.error('导出失败');
                    this.downloadLoading = false;
                });
            return this.checkOutData;
        },

        // 导出表格非后台查询的特例 成本计算页面
        checkOutForCostCunting() {
            let object = {};
            let total = 0;
            let temp = [];
            for (let i = 0; i < this.countListForm.length; i++) {
                let listObject = {};
                listObject = this.countListForm[i];
                temp.push(listObject);
                total += this.countListForm[i].TotalCount;
            }
            object.cpuNum = '';
            object.memSize = '';
            object.storageType = '';
            object.diskSize = '';
            object.otherPrice = '';
            object.TotalCount = total + '元';
            temp.push(object);
            this.checkOutData = temp;
            return this.checkOutData;
        },
        async checkOut_get() {
            this.downloadLoading = true;
            await service
                .get(this.url)
                .then(res => {
                    this.downloadLoading = false;
                    this.checkOutData = this.analysisResult(res.data.data);
                }).catch(() => {
                    this.$message.error('导出失败');
                    this.downloadLoading = false;
                });
            return this.checkOutData;
        },
    }
};
